<?php

defined('SYSPATH') or die('No direct script access.');

/**
 * 抢购数据导入
 * @version $Id: transfer.php 4 2011-12-15 09:28:04Z 周永隆 $
 */
class CLI_Activity_Qiang_Transfer extends CLI
{

    protected $_run_time = 60; // 估计运行间隔

    public function main() {
        try{
            $bench_start = microtime(TRUE);

            $list = DB::select('*')
                ->from('group_goods')
                ->where('publish', '=', '1')
                ->where('promotion_type', '=', 'QIANG')
                ->where('new_id', '=', 0)
                ->fetch_all();
            
            foreach ($list as $row)
            {
                $insert_fields = array(
                    'goods_id' => $row['goods_id'],
                    'title' => $row['title'],
                    'old_stock' => $row['max_quantity'],
                    'stock' => 0,
                    'sku_id_list' => $row['sku_id_list'],
                    'price' => $row['price'],
                    'market_price' => $row['market_price'],
                    'start_time' => $row['start_date'],
                    'end_time' => $row['end_date'],
                    'image_md5' => $row['image_md5'],
                    'home_image' => $row['home_image'],
                    'paying_num' => $row['paying_num'],
                    'payed_num' => $row['payed_num'],
                    'order_num' => 0,
                    'weight_order' => $row['weight_order'],
                    'weight_paying' => $row['weight_paying'],
                    'weight_paid' => $row['weight_paid'],
                    'base_num' => $row['base_num'],
                    'sort_order' => $row['sort_order'],
                    'qiang_type' => 1,
                    'selling_point' => $row['selling_point'],
                    'ac_status' => 1,
                    'limit_phone' => $row['limit_phone'],
                    'limit_person' => $row['limit_person'],
                    'limit_ip' => $row['limit_ip'],
                    'limit_member' => $row['limit_quantity'],
                    'created' => $row['addtime'],
                    'modified' => $row['addtime'],
                    'notes' => $row['notes'],
                    'is_transfer' => 1,
                );

                $result = DB::insert('activity_qiang', array_keys($insert_fields))
                    ->values($insert_fields)
                    ->execute();

                DB::update('group_goods')
                    ->set(array(
                        'new_id' => $result[0],
                    ))
                    ->where('id', '=', $row['id'])
                    ->execute();

                DB::insert('activity_fields', array('ac_id', 'ac_type', 'description'))
                    ->values(array(
                        'ac_id' => $result[0],
                        'ac_type' => 2,
                        'description' => $row['description'],
                    ))
                    ->execute();
            }
            
            $bench_end = microtime(TRUE);
            $eclipse_time = round($bench_end - $bench_start, 3);
            $log = "更新活动转发数据 执行用时  {$eclipse_time} 秒.".date('Y-m-d H:i:s');
            CLI::output("\n" . $log);
        }
        catch(Exception $e)
        {
            DB::delete('cli_running')
                ->where('class', '=', get_class($this))
                ->execute();
            CLI::output("\n" . $e->getMessage());
        }
    }

    /**
     * 构造方法
     *
     * @param  CLI_Args  $args
     * @return void
     */
    public function __construct(CLI_Args $args = NULL)
    {

        $this->args = $args;
        if(DB::select('addtime')
            ->from('cli_running')
            ->where('class', '=', get_class($this))
            ->where('addtime', '>', time() - $this->_run_time)
            ->fetch_one())
        {
            CLI::output("\n 此分析器正在运行中");
            die();
        }
        else
        {
            $this->_runed = true;
            DB::insert('cli_running', array('class', 'addtime'))
                ->values(array(get_class($this), time()))
                ->execute();
        }
    }

    /**
     * 删除运行状态
     */
    public function  __destruct()
    {
        if($this->_runed)
        {
            DB::delete('cli_running')->where('class', '=', get_class($this))->execute();
        }
    }

}